<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>/net/nas/exports/srv/www/webapps/Projects/Java/CS222/Seminar_1/AppendixA18/src/RandomTwo.java.html</title>
<meta name="Generator" content="Vim/7.1">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body bgcolor="#ffffff" text="#000000"><font face="monospace">
<font color="#0000ff">/**</font><br>
<font color="#0000ff">&nbsp;*</font><font color="#6a5acd">&nbsp;RandomTwo.java</font><br>
<font color="#0000ff">&nbsp;*</font><font color="#6a5acd">&nbsp;write a detailed explanation for each of the lines 5, 6 12, 15, 16, 18-30</font><br>
<font color="#0000ff">&nbsp;*</font><br>
<font color="#0000ff">&nbsp;</font><font color="#0000ff">* </font><font color="#6a5acd">@author</font><font color="#0000ff">: Sun Microsystems</font><br>
<font color="#0000ff">&nbsp;*&nbsp;&nbsp;Editor: John Thomason</font><br>
<font color="#0000ff">&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;Date: Nov 6, 2008 / 7:22:00 AM</font><br>
<font color="#0000ff">&nbsp;*&nbsp;&nbsp; Class: CS222 Java II</font><br>
<font color="#0000ff">&nbsp;*/</font><br>
<br>
<font color="#0000ff">/**</font><br>
<font color="#0000ff">&nbsp;*</font><font color="#6a5acd">&nbsp;RandomTwo Class</font><br>
<font color="#6a5acd">&nbsp;</font><font color="#0000ff">*/</font><br>
<font color="#2e8b57"><b>public</b></font>&nbsp;<font color="#2e8b57"><b>class</b></font>&nbsp;RandomTwo {<br>
<br>
&nbsp;&nbsp;<font color="#0000ff">/**</font><br>
<font color="#0000ff">&nbsp;&nbsp; *</font><font color="#6a5acd">&nbsp;Application Entry Point</font><br>
<font color="#0000ff">&nbsp;&nbsp; </font><font color="#0000ff">* </font><font color="#6a5acd">@param</font><font color="#008b8b">&nbsp;args</font><font color="#0000ff">&nbsp;The command line arguments</font><br>
<font color="#0000ff">&nbsp;&nbsp; */</font><br>
&nbsp;&nbsp;<font color="#2e8b57"><b>public</b></font>&nbsp;<font color="#2e8b57"><b>static</b></font>&nbsp;<font color="#2e8b57"><b>void</b></font>&nbsp;main(String[]&nbsp;args)&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">/**</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; *</font><font color="#6a5acd">&nbsp;Create an instance of the ObjectTwo class, and store</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; *</font><font color="#6a5acd">&nbsp;the reference to the new object in the identifier 'two'.</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * The new object is stored on the heap, while the reference</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * is on the stack.</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; */</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;ObjectTwo two = <font color="#a52a2a"><b>new</b></font>&nbsp;ObjectTwo();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">//Line 5;</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">/**</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; *</font><font color="#6a5acd">&nbsp;Call the 'go' method from the 'two' object.</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; */</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;two.go();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000ff">//Line 6;</font><br>
<br>
&nbsp;&nbsp;}<font color="#0000ff">//end main()</font><br>
<br>
}<font color="#0000ff">//end RandomTwo class</font><br>
<br>
<font color="#0000ff">/**</font><br>
<font color="#0000ff">&nbsp;*</font><font color="#6a5acd">&nbsp;ObjectTwo Class</font><br>
<font color="#6a5acd">&nbsp;</font><font color="#0000ff">*/</font><br>
<font color="#2e8b57"><b>class</b></font>&nbsp;ObjectTwo {<br>
&nbsp;&nbsp;<br>
&nbsp;&nbsp;<font color="#0000ff">/**</font><br>
<font color="#0000ff">&nbsp;&nbsp; *</font><font color="#6a5acd">&nbsp;go()</font><br>
<font color="#0000ff">&nbsp;&nbsp; *</font><font color="#6a5acd">&nbsp;The method called from the main method above.</font><br>
<font color="#0000ff">&nbsp;&nbsp; */</font><br>
&nbsp;&nbsp;<font color="#2e8b57"><b>public</b></font>&nbsp;<font color="#2e8b57"><b>void</b></font>&nbsp;go(){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000ff">//Line 12;</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2e8b57"><b>int</b></font>&nbsp;max = <font color="#ff00ff">10</font>;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">/**</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; *</font><font color="#6a5acd">&nbsp;Generate two separate random values between 1 and max (inclusive).</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * </font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * The Math class is part of java.lang, so it does not require importing.</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * </font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * Math.random() returns a double between 0.0 and less than 1.</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * You must use parenthesis around (Math.random() * max) to modify</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * the order in-which the expression is evaluated. </font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * </font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * Casting has a higher precedence than multiplication, so the parenthesis</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * are mandatory to evaluate (Math.random() * max) before casting to an int. </font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * Otherwise, you will ALWAYS generate zero(0) as the random value</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * (I've done that once or twice!).</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * </font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * (int)(Math.random() * max) returns a random integer between zero(0)</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * and (max - 1). By adding one(1) to the result, we store a random</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * value between 1 and max (inclusive) in 'rand1' and 'rand2'.</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; */</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2e8b57"><b>int</b></font>&nbsp;rand1 = (<font color="#2e8b57"><b>int</b></font>)(Math.random()&nbsp;* max)&nbsp;+ <font color="#ff00ff">1</font>; <font color="#0000ff">//Line 15;</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2e8b57"><b>int</b></font>&nbsp;rand2 = (<font color="#2e8b57"><b>int</b></font>)(Math.random()&nbsp;* max)&nbsp;+ <font color="#ff00ff">1</font>; <font color="#0000ff">//Line 16;</font><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">/**</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; *</font><font color="#6a5acd">&nbsp;Evaluate the values in rand1 and rand2, then display the appropriate</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; *</font><font color="#6a5acd">&nbsp;message (Bananas, Oranges, Pears).</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * </font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * If the value of rand1 is less than or equal to three(3), then display</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * 'Bananas' regardless of the value in rand2.</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * </font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * If the value of rand1 is greater than three(3), then evaluate rand2.</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * </font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * If the value of rand1 is greater than three(3), AND the value of rand2</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * is less than or equal to five(5), then display 'Oranges'</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * </font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * If the value of rand1 is greater than three(3), AND the value of rand2</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * is greater than five(5), then display 'Pears'</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; */</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#a52a2a"><b>if</b></font>(&nbsp;rand1 &lt;= <font color="#ff00ff">3</font>&nbsp;){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000ff">//Line 18;</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<font color="#ff00ff">&quot;Bananas&quot;</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#a52a2a"><b>else</b></font>&nbsp;{<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#a52a2a"><b>if</b></font>(&nbsp;rand2 &lt;= <font color="#ff00ff">5</font>&nbsp;){<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<font color="#ff00ff">&quot;Oranges&quot;</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#a52a2a"><b>else</b></font>&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<font color="#ff00ff">&quot;Pears&quot;</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<font color="#0000ff">//if()</font><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<font color="#0000ff">//if()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Line 30;</font><br>
<br>
&nbsp;&nbsp;}<font color="#0000ff">//go()</font><br>
&nbsp;&nbsp;<br>
}<font color="#0000ff">//End ObjectTwo class</font><br>
</font></body>
</html>
